package com.nine.mq.controller;

import com.nine.mq.config.constanst.MqConstanst;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/p")
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @RequestMapping("/send")
    public String send(String msg) {
        String messageId = UUID.randomUUID().toString();
        String current = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("data", msg);
        map.put("current", current);
        rabbitTemplate.convertAndSend(MqConstanst.EXCHANGE_NAME, MqConstanst.ROUTING_KEY, map, new CorrelationData(UUID.randomUUID().toString()));
        return "ok";
    }

}
